<!--
 * @Autor: 田甜
 * @Date: 2020-06-11 15:16:41
-->
<template>
  <div class="page-account">
    <div v-if="showI18n" class="page-account-header">
      <i-header-i18n />
    </div>
    <div class="page-account-container">
      <div class="page-account-top">
        <div class="page-account-top-logo">
          <img src="@/assets/images/logo.png" alt="logo" />
        </div>
        <div class="page-account-top-desc">
          iView Admin Pro 企业级中台前端/设计解决方案
        </div>
      </div>
      <Login @on-submit="handleSubmit">
        <UserName name="username" :value="username" />
        <Password name="password" :value="password" enter-to-submit />
        <div class="page-account-auto-login">
          <Checkbox v-model="autoLogin" size="large">{{
            $t("page.login.remembers")
          }}</Checkbox>
          <a href="">{{ $t("page.login.forgot") }}</a>
        </div>
        <Submit>{{ $t("page.login.submit") }}</Submit>
      </Login>
      <div class="page-account-other">
        <span>{{ $t("page.login.other") }}</span>
        <img src="@/assets/svg/icon-social-wechat.svg" alt="wechat" />
        <img src="@/assets/svg/icon-social-dingding.svg" alt="dingding" />
        <img src="@/assets/svg/icon-social-qq.svg" alt="qq" />
        <!-- <router-link class="page-account-register" :to="{ name: 'register' }">{{ $t('page.login.signup') }}</router-link> -->
      </div>
    </div>
    <i-copyright />
  </div>
</template>
<script>
    import iCopyright from '@/components/copyright';
    import { mapActions } from 'vuex';
    import mixins from '../mixins';
    import util from '@/libs/util';
    export default {
        mixins: [mixins],
        components: { iCopyright },
        data () {
            return {
                autoLogin: true,
                username: '',
                password: ''
            };
        },
        beforeMount () {
            // if (window.localStorage.getItem('loginInfo')) {
            //     let obj = JSON.parse(window.localStorage.getItem('loginInfo'))
            //     this.username = obj.username
            //     this.password = obj.password
            // }
            if(util.cookies.get('username')&&util.cookies.get('password')){
              let username = util.cookies.get('username');
              this.username = username;
              let password= util.cookies.get('password');
              // let enc = window.btoa(password) //编码成base64的
              let dec = window.atob(password) //把base64解码
              this.password = dec;
            }
        },
        methods: {
            ...mapActions('admin/account', ['login']),
            /**
             * @description 登录
             * 表单校验已有 iView Pro 自动完成，如有需要修改，请阅读 iView Pro 文档
             */
            handleSubmit (valid, values) {
                if (valid) {
                    const { username, password } = values;
                    this.login({
                        username,
                        password
                    }).then(() => {
                        // 重定向对象不存在则返回顶层路径
                        if (this.autoLogin) {
                            // let userLogin = {
                            //     username: values.username,
                            //     password: values.password
                            // }
                            // window.localStorage.setItem('loginInfo', JSON.stringify(userLogin))
                            let enc = window.btoa(password) 
                            util.cookies.set('username', username);
                            util.cookies.set('password', enc);
                        } else {
                            // if (window.localStorage.getItem('loginInfo')) {
                            //     window.localStorage.removeItem('loginInfo')
                            // }
                            if (util.cookies.get('username')&&util.cookies.get('password')) {
                              util.cookies.remove('username');
                              util.cookies.remove('password');
                            }
                        }
                        // 重定向对象不存在则返回顶层路径
                        this.$router.replace(this.$route.query.redirect || '/');
                    });
                }
            }
        },
        mounted () {
            // if (window.localStorage.getItem('loginInfo')) {
            //     let obj = JSON.parse(window.localStorage.getItem('loginInfo'));
            //     this.username = obj.mobile;
            //     this.password = obj.password;
            // }
            if(util.cookies.get('username')&&util.cookies.get('password')){
              let username = util.cookies.get('username');
              this.username = username;
              let password= util.cookies.get('password');
              // let enc = window.btoa(password) //编码成base64的
              let dec = window.atob(password) //把base64解码
              this.password = dec;
            }
        }
    };
</script>
